/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.config;

import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class PackageFilter implements TypeFilter {
    protected String root;
    protected final List<String> scanPackages;

    public PackageFilter() {
        this.scanPackages = new LinkedList<>();
        init();
    }

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) {
        // class information currently being scanned
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        String className = classMetadata.getClassName();
        if (!className.startsWith(this.root)) {
            return false;
        }

        String matchPackage = className.substring(this.root.length());
        // filter packages
        for (String path : this.scanPackages) {
            if (matchPackage.startsWith(path)) {
                return false;
            }
        }
        // exclude
        return true;
    }

    public void init() {
        this.root = "org.openislands.oi.";
        this.scanPackages.addAll(Arrays.asList(
                "config",
                "manager",
                "rpc",
                "scheduling",
                "operator",
                "dao.injector"
        ));
    }

    public static class Apiserver extends PackageFilter {
        public void init() {
            super.init();
            this.scanPackages.addAll(Arrays.asList(
                    "bootstrap.Apiserver",
                    "apiserver"
            ));
        }
    }

    public static class Master extends PackageFilter {
        public void init() {
            super.init();
            this.scanPackages.addAll(Arrays.asList(
                    "bootstrap.Master",
                    "master"
            ));
        }
    }

    public static class Worker extends PackageFilter {
        public void init() {
            super.init();
            this.scanPackages.addAll(Arrays.asList(
                    "bootstrap.Worker",
                    "worker"
            ));
        }
    }
}
